package com.fzg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.common.model.param.diary.AddDiaryParam;
import com.fzg.common.model.param.diary.CommentPublishParam;
import com.fzg.common.model.param.diary.GetDiaryListParam;
import com.fzg.common.model.param.diary.GetOtherUserDiaryList;
import com.fzg.common.model.result.diary.DiaryComment;
import com.fzg.common.model.result.diary.GetDiaryAllInfoByAidResult;
import com.fzg.common.model.result.diary.GetDiaryListResult;
import com.fzg.common.model.result.diary.GetRecommendDiaryList;
import com.fzg.common.tool.JwtUtil;
import com.fzg.common.tool.Medthods;
import com.fzg.common.tool.ToolForDateTime;
import com.fzg.entity.*;
import com.fzg.mapper.*;
import com.fzg.service.DiaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzg
 * @since 2022-06-20
 */
@Service
public class DiaryServiceImpl extends ServiceImpl<DiaryMapper, Diary> implements DiaryService {

    @Autowired
    private DiaryMapper diaryMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserGuanzhuMapper userGuanzhuMapper;

    @Autowired
    private DiaryDianzanMapper diaryDianzanMapper;

    @Autowired
    private DiaryCommentMapper diaryCommentMapper;

    @Autowired
    private DiaryCommentDianzanMapper diaryCommentDianzanMapper;

    @Autowired
    private UserCollectMapper userCollectMapper;

    /**
     * 获取日记列表
     * @param param 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper getDiaryListApi(HttpServletRequest request, GetDiaryListParam param) {

        // 获取token
        String token = request.getHeader("token");
        Integer userAid = JwtUtil.getTokenId(token);
        ArrayList<GetDiaryListResult> result = new ArrayList<>();

        if (param.getTimerValue().equals("-1")){
            // 查询全部
            if (param.getDiaryTimeTypeValue().equals(0)){
                List<Diary> diaryListAll = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, userAid)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListAll) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }else if (param.getDiaryTimeTypeValue().equals(1)){
                // 查询本周
                LocalDateTime now = LocalDateTime.now();
                // 本周第一天
                LocalDateTime weekFirstDay = now.with(DayOfWeek.MONDAY);
                List<Diary> diaryListWeek = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, userAid)
                        .between(Diary::getCreateTime, weekFirstDay, now)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListWeek) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }else if (param.getDiaryTimeTypeValue().equals(2)){  // 查询本月
                LocalDateTime now = LocalDateTime.now();
                // 本月第一天
                LocalDateTime monthFirstDay = now.with(TemporalAdjusters.firstDayOfMonth());
                List<Diary> diaryListMonth = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, userAid)
                        .between(Diary::getCreateTime, monthFirstDay, now)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListMonth) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }else if (param.getDiaryTimeTypeValue().equals(3)){
                LocalDateTime now = LocalDateTime.now();
                // 今年第一天
                LocalDateTime CurrentYearFirstDay = now.with(TemporalAdjusters.firstDayOfYear());
                List<Diary> diaryListCurrentYear = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, userAid)
                        .between(Diary::getCreateTime, CurrentYearFirstDay, now)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListCurrentYear) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }else if (param.getDiaryTimeTypeValue().equals(4)){  // 去年
                LocalDateTime now = LocalDateTime.now();
                String lastYeay = String.valueOf(Integer.parseInt(now.toString().substring(0, 4)) - 1);
                List<Diary> diaryListLastYeay = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, userAid)
                        .likeRight(Diary::getCreateTime, lastYeay)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListLastYeay) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }
        }else {  // 时间选择器有值
            List<Diary> diaryList = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                    .eq(Diary::getUserAid, userAid)
                    .likeRight(Diary::getCreateTime, param.getTimerValue())
                    .orderByDesc(Diary::getCreateTime)
            );
            for (Diary diary : diaryList) {
                GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                diaryReuslt.setDiaryAid(diary.getAid());
                diaryReuslt.setUserAid(diary.getUserAid());
                // 日记图片有很多张或者没有或者只有 一张
                if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                    diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                }else {
                    boolean flag = false;
                    for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                        String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                        if (ch.equals(";")) {
                            flag = true;
                            break;
                        }
                    }
                    if (flag){ // 说明有两张及两张以上的图片
                        String[] picUrls = diary.getDiaryImgUrl().split(";");
                        diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                    }else { // 说明只有一张
                        diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                    }
                }
                diaryReuslt.setContent(diary.getContent());
                String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                if (timeToString.charAt(5) == '0') {
                    diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6, 7)));
                } else {
                    diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5, 7)));
                }
                if (timeToString.charAt(8) == '0') {
                    diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9, 10)));
                } else {
                    diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8, 10)));
                }
                diaryReuslt.setDayTime(timeToString.substring(11, 16));
                result.add(diaryReuslt);
            }
            return ResponseWrapper.markSuccess(result);
        }
        return ResponseWrapper.markSuccess(new ArrayList<>());
    }

    /**
     * 获取(其他人的)日记列表
     *
     * @param param 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper getOtherUserDiaryList(GetOtherUserDiaryList param) {

        ArrayList<GetDiaryListResult> result = new ArrayList<>();

        if (param.getTimerValue().equals("-1")){
            // 查询全部
            if (param.getDiaryTimeTypeValue().equals(0)){
                List<Diary> diaryListAll = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, param.getUserAid())
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListAll) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }else if (param.getDiaryTimeTypeValue().equals(1)){
                // 查询本周
                LocalDateTime now = LocalDateTime.now();
                // 本周第一天
                LocalDateTime weekFirstDay = now.with(DayOfWeek.MONDAY);
                List<Diary> diaryListWeek = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, param.getUserAid())
                        .between(Diary::getCreateTime, weekFirstDay, now)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListWeek) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }else if (param.getDiaryTimeTypeValue().equals(2)){  // 查询本月
                LocalDateTime now = LocalDateTime.now();
                // 本月第一天
                LocalDateTime monthFirstDay = now.with(TemporalAdjusters.firstDayOfMonth());
                List<Diary> diaryListMonth = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, param.getUserAid())
                        .between(Diary::getCreateTime, monthFirstDay, now)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListMonth) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }else if (param.getDiaryTimeTypeValue().equals(3)){
                LocalDateTime now = LocalDateTime.now();
                // 今年第一天
                LocalDateTime CurrentYearFirstDay = now.with(TemporalAdjusters.firstDayOfYear());
                List<Diary> diaryListCurrentYear = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, param.getUserAid())
                        .between(Diary::getCreateTime, CurrentYearFirstDay, now)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListCurrentYear) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }else if (param.getDiaryTimeTypeValue().equals(4)){  // 去年
                LocalDateTime now = LocalDateTime.now();
                String lastYeay = String.valueOf(Integer.parseInt(now.toString().substring(0, 4)) - 1);
                List<Diary> diaryListLastYeay = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                        .eq(Diary::getUserAid, param.getUserAid())
                        .likeRight(Diary::getCreateTime, lastYeay)
                        .orderByDesc(Diary::getCreateTime)
                );
                for (Diary diary : diaryListLastYeay) {
                    GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                    diaryReuslt.setDiaryAid(diary.getAid());
                    diaryReuslt.setUserAid(diary.getUserAid());
                    // 日记图片有很多张或者没有或者只有 一张
                    if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                        diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                    }else {
                        boolean flag = false;
                        for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                            String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                            if (ch.equals(";")) {
                                flag = true;
                                break;
                            }
                        }
                        if (flag){ // 说明有两张及两张以上的图片
                            String[] picUrls = diary.getDiaryImgUrl().split(";");
                            diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                        }else { // 说明只有一张
                            diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                        }
                    }
                    diaryReuslt.setContent(diary.getContent());
                    String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                    if (timeToString.charAt(5) == '0'){
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6,7)));
                    }else {
                        diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5,7)));
                    }
                    if (timeToString.charAt(8) == '0'){
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9,10)));
                    }else {
                        diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8,10)));
                    }
                    diaryReuslt.setDayTime(timeToString.substring(11,16));
                    result.add(diaryReuslt);
                }
                return ResponseWrapper.markSuccess(result);
            }
        }else {  // 时间选择器有值
            List<Diary> diaryList = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                    .eq(Diary::getUserAid, param.getUserAid())
                    .likeRight(Diary::getCreateTime, param.getTimerValue())
                    .orderByDesc(Diary::getCreateTime)
            );
            for (Diary diary : diaryList) {
                GetDiaryListResult diaryReuslt = new GetDiaryListResult();
                diaryReuslt.setDiaryAid(diary.getAid());
                diaryReuslt.setUserAid(diary.getUserAid());
                // 日记图片有很多张或者没有或者只有 一张
                if (diary.getDiaryImgUrl() == null){  // 如果该日记没有图片
                    diaryReuslt.setDiaryImg("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F052420110515%2F200524110515-11-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1658383473&t=2afc825ebaf15cf0c11775764902135c");
                }else {
                    boolean flag = false;
                    for (int i = 0; i < diary.getDiaryImgUrl().length(); i++) {
                        String ch = String.valueOf(diary.getDiaryImgUrl().charAt(i));
                        if (ch.equals(";")) {
                            flag = true;
                            break;
                        }
                    }
                    if (flag){ // 说明有两张及两张以上的图片
                        String[] picUrls = diary.getDiaryImgUrl().split(";");
                        diaryReuslt.setDiaryImg(picUrls[0]);  // 取第一张
                    }else { // 说明只有一张
                        diaryReuslt.setDiaryImg(diary.getDiaryImgUrl());
                    }
                }
                diaryReuslt.setContent(diary.getContent());
                String timeToString = ToolForDateTime.LocalDateTimeToString(diary.getCreateTime());
                if (timeToString.charAt(5) == '0') {
                    diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(6, 7)));
                } else {
                    diaryReuslt.setMonth(Integer.parseInt(timeToString.substring(5, 7)));
                }
                if (timeToString.charAt(8) == '0') {
                    diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(9, 10)));
                } else {
                    diaryReuslt.setHaoDate(Integer.parseInt(timeToString.substring(8, 10)));
                }
                diaryReuslt.setDayTime(timeToString.substring(11, 16));
                result.add(diaryReuslt);
            }
            return ResponseWrapper.markSuccess(result);
        }
        return ResponseWrapper.markSuccess(new ArrayList<>());
    }

    /**
     * 根据日记aid后端获取日记相关的所有信息
     *
     * @param aid 日记aid
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper getDiaryAllInfoByAid(HttpServletRequest request, Integer aid) {

        // 返回结果
        GetDiaryAllInfoByAidResult result = new GetDiaryAllInfoByAidResult();

        Diary diary = diaryMapper.selectById(aid);

        // 这条日记的作者用户aid
        result.setDiaryAuthorUserAid(diary.getUserAid());

        // 这条日记的作者头像
        User user = userMapper.selectById(diary.getUserAid());
        if (user.getAvatar() != null){
            result.setUserImg(user.getAvatar());
        }else {
            // 用户头像没有就设置一个默认头像
            result.setUserImg("https://img1.baidu.com/it/u=905482077,1717804812&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
        }

        // 这条日记的作者昵称
        if (user.getNickName() != null){
            result.setNickName(user.getNickName());
        }else {
            // 作者昵称为空就设置为用户名
            result.setNickName(user.getUserName());
        }

        // 这条日记标签
        if ( (!diary.getTag().equals("")) && (diary.getTag() != null)){
            result.setDiaryTag(diary.getTag());
        }else {
            result.setDiaryTag("");
        }

        // 这条日记是否是当前登陆的作者写的
        // 获取token
        String token = request.getHeader("token");
        // 当前登陆用户的aid
        Integer currentLoginUserAid = JwtUtil.getTokenId(token);
        result.setIsDiaryAuthorSelf(currentLoginUserAid.equals(user.getAid()));

        // 当前登陆用户是否收藏了这条日记
        List<UserCollect> userCollects = userCollectMapper.selectList(new LambdaQueryWrapper<UserCollect>()
                .eq(UserCollect::getUserAid, currentLoginUserAid)
                .eq(UserCollect::getCollectType, 1)
                .eq(UserCollect::getContentAid, diary.getAid()));
        result.setIsCollected(userCollects.size() > 0);

        // 日记的内容
        result.setDiaryContentCopywritting(diary.getContent());

        // 这条日记的评论数量
        result.setCommentCount(diaryCommentMapper.selectList(
                new LambdaQueryWrapper<com.fzg.entity.DiaryComment>()
                    .eq(com.fzg.entity.DiaryComment::getDiaryAid,aid)
        ).size());

        // 日记包含的图片地址（有可能没有，有可能只有一张，有可能有一张以上：地址之间以;隔开）
        result.setDiaryDetailImgList(Medthods.imageStrSplitToArrayList(diary.getDiaryImgUrl()));

        // 日记的发布时间(2022年12月1日 14:52)
        LocalDateTime time = diary.getCreateTime();
        // 转string
        System.out.println(time.toString().substring(11,16));
        // LocalDateTime转yyyy-MM-dd
        String time1 = ToolForDateTime.LocalDateTimeToStringToDate(time);
        // yyyy-MM-dd转x年x月x日
        String timer2 = ToolForDateTime.StringDateToYearMonthDay(time1);
        // 时间部分
        String time3 = time.toString().substring(11, 16);
        // 最后返回的发布时间
        result.setDiaryTime(timer2 + " " + time3);

        // 日记的点赞数量
        result.setLikesNumber(diary.getLikesNumber());

        // 是否关注了这条日记的作者
        List<UserGuanzhu> myselfGuanzhuUserList = userGuanzhuMapper.selectList(new LambdaQueryWrapper<UserGuanzhu>()
                .eq(UserGuanzhu::getGuanzhuUserAid, currentLoginUserAid)
        );
        boolean isGuanzhu = false;
        for (UserGuanzhu userGuanzhu : myselfGuanzhuUserList) {
            if (user.getAid().equals(userGuanzhu.getBeiguanzhuUserAid())){
                isGuanzhu = true;
                break;
            }
        }
        result.setIsGuanzhu(isGuanzhu);

        // 是否点赞了这条日记
        List<DiaryDianzan> diaryDianzans = diaryDianzanMapper.selectList(new LambdaQueryWrapper<DiaryDianzan>()
                .eq(DiaryDianzan::getUserAid, currentLoginUserAid)
        );
        boolean isDianZan = false;
        for (DiaryDianzan diaryDianzan : diaryDianzans) {
            if (diaryDianzan.getDiaryAid().equals(diary.getAid())){
                isDianZan = true;
                break;
            }
        }
        result.setFabulous(isDianZan);

        // 日记的评论列表
        result.setDiaryCommentList(this.getDiaryCommentTree(diary,currentLoginUserAid));

        return ResponseWrapper.markSuccess(result);
    }

    /**
     * 点击关注作者
     *
     * @param request 参数
     * @param aid 用户aid
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper concernUser(HttpServletRequest request, Integer aid) {
        // 获取当前登陆的用户aid
        Integer currentLoginUserAid = JwtUtil.getTokenId(request.getHeader("token"));

        // 根据传过来的aid获取这条日记的作者aid
        Integer userAid = diaryMapper.selectById(aid).getUserAid();

        // 当前这个登陆的用户关注这条日记的作者
        int insert = userGuanzhuMapper.insert(new UserGuanzhu() {{
            setGuanzhuUserAid(currentLoginUserAid);
            setBeiguanzhuUserAid(userAid);
        }});
        if (insert > 0){
            return ResponseWrapper.markCustomSuccess("关注成功！");
        }else {
            return ResponseWrapper.markCustomError("关注失败！请稍后再试！");
        }
    }

    /**
     * 取消关注作者
     *
     * @param request 参数
     * @param aid 用户aid
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper cancelConcernUser(HttpServletRequest request, Integer aid) {
        int delete = userGuanzhuMapper.delete(new LambdaQueryWrapper<UserGuanzhu>()
                .eq(UserGuanzhu::getGuanzhuUserAid, JwtUtil.getTokenId(request.getHeader("token")))
                .eq(UserGuanzhu::getBeiguanzhuUserAid, diaryMapper.selectById(aid).getUserAid())
        );
        if (delete > 0){
            return ResponseWrapper.markCustomSuccess("已取消关注！");
        }else {
            return ResponseWrapper.markError();
        }
    }

    /**
     * 当前登陆的用户给这条日记点赞
     *
     * @param request 参数
     * @param aid 前登陆的用户aid
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper thumbsUpToDiary(HttpServletRequest request, Integer aid) {
        // 给这条日记赞的数量加1
        Diary diary = diaryMapper.selectById(aid);
        Diary diary1 = new Diary();
        BeanUtils.copyProperties(diary,diary1);
        diary1.setLikesNumber(diary.getLikesNumber() + 1);
        diaryMapper.updateById(diary1);

        // 点赞
        diaryDianzanMapper.insert(new DiaryDianzan(){{
            setUserAid(JwtUtil.getTokenId(request.getHeader("token")));
            setDiaryAid(aid);
        }});
        return ResponseWrapper.markSuccess();
    }

    /**
     * 当前登陆用户给这条日记取消点赞
     *
     * @param request 参数
     * @param aid 当前登陆用户aid
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper cancelThumbsUpToDiary(HttpServletRequest request, Integer aid) {
        // 给这条日记赞的数量-1
        Diary diary = diaryMapper.selectById(aid);
        Diary diary1 = new Diary();
        BeanUtils.copyProperties(diary,diary1);
        if (diary.getLikesNumber() > 0){
            diary1.setLikesNumber(diary.getLikesNumber() - 1);
        }else {
            diary1.setLikesNumber(0);
        }
        diaryMapper.updateById(diary1);

        // 取消点赞
        diaryDianzanMapper.delete(new LambdaQueryWrapper<DiaryDianzan>()
                .eq(DiaryDianzan::getUserAid,JwtUtil.getTokenId(request.getHeader("token")))
                .eq(DiaryDianzan::getDiaryAid,aid)
        );
        return ResponseWrapper.markSuccess();
    }

    /**
     * 点击评论里面的赞，改变赞的状态
     *
     * @param request 参数
     * @param aid 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper clickCommentLike(HttpServletRequest request, Integer aid) {
        // 获取当前登陆的用户aid
        Integer currentUserAid = JwtUtil.getTokenId(request.getHeader("token"));

        DiaryCommentDianzan diaryCommentDianzan = diaryCommentDianzanMapper.selectOne(new LambdaQueryWrapper<DiaryCommentDianzan>()
                .eq(DiaryCommentDianzan::getUserAid, currentUserAid)
                .eq(DiaryCommentDianzan::getDiaryCommentAid, aid)
        );
        if (diaryCommentDianzan != null){
            System.out.println("fzg=>  " + diaryCommentDianzan.getAid());
            // 说明已经赞过了，
            diaryCommentDianzanMapper.delete(new LambdaQueryWrapper<DiaryCommentDianzan>()
                    .eq(DiaryCommentDianzan::getUserAid, currentUserAid)
                    .eq(DiaryCommentDianzan::getDiaryCommentAid, aid)
            );
            // 获赞数-1
            com.fzg.entity.DiaryComment diaryComment = diaryCommentMapper.selectById(aid);
            diaryComment.setLikesCount(diaryComment.getLikesCount() - 1);
            diaryCommentMapper.updateById(diaryComment);
        }else {
            // 说明还没赞过
            diaryCommentDianzanMapper.insert(new DiaryCommentDianzan(){{
                setUserAid(currentUserAid);
                setDiaryCommentAid(aid);
            }});
            // 日记评论的获赞数+1
            com.fzg.entity.DiaryComment diaryComment = diaryCommentMapper.selectById(aid);
            diaryComment.setLikesCount(diaryComment.getLikesCount() + 1);
            diaryCommentMapper.updateById(diaryComment);
        }
        return ResponseWrapper.markSuccess();
    }

    /**
     * 发表评论
     *
     * @param request 参数
     * @param param 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper diaryCommentPublish(HttpServletRequest request, CommentPublishParam param) {
        int insert = diaryCommentMapper.insert(new com.fzg.entity.DiaryComment() {{
            setUserAid(JwtUtil.getTokenId(request.getHeader("token")));
            setDiaryAid(param.getDiaryAid());
            setComment(param.getCommentContent());
            setParentAid((param.getCommentParentAid() == null || param.getCommentParentAid() == -1) ? null : param.getCommentParentAid());
            setLikesCount(0);
        }});
        if (insert > 0){
            return ResponseWrapper.markCustomSuccess("发表成功！");
        }else {
            return ResponseWrapper.markCustomError("发表失败！");
        }
    }

    /**
     * 获取系统给当前登陆的用户推荐的所有日记列表
     *
     * @param request 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper getRecommentDiaryList(HttpServletRequest request) {
        List<Diary> diaries = diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                .orderByDesc(Diary::getCreateTime)
        );
        ArrayList<GetRecommendDiaryList> result = new ArrayList<>();

        for (Diary diary : diaries) {
            GetRecommendDiaryList resultItem = new GetRecommendDiaryList();
            resultItem.setDiaryAid(diary.getAid());
            User user = userMapper.selectById(diary.getUserAid());
            resultItem.setUserAid(user.getAid());
            resultItem.setUserName(user.getUserName());
            resultItem.setUserNickName(user.getNickName() == null ? user.getUserName() : user.getNickName());
            resultItem.setAvatar(user.getAvatar() == null ? "https://img1.baidu.com/it/u=3866475743,1893693583&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400" : user.getAvatar());

            // 日记的发布时间(2022年12月1日 14:52)
            LocalDateTime time = diary.getCreateTime();
            // LocalDateTime转yyyy-MM-dd
            String time1 = ToolForDateTime.LocalDateTimeToStringToDate(time);
            // yyyy-MM-dd转x年x月x日
            String timer2 = ToolForDateTime.StringDateToYearMonthDay(time1);
            // 时间部分
            String time3 = time.toString().substring(11, 16);
            // 最后返回的发布时间
            resultItem.setTime(timer2 + " " + time3);

            // 这条日记的内容
            resultItem.setContent(diary.getContent() == null ? "" : diary.getContent());

            // 这条日记的图片list
            // 日记包含的图片地址（有可能没有，有可能只有一张，有可能有一张以上：地址之间以;隔开）
            resultItem.setImageList(Medthods.imageStrSplitToArrayList(diary.getDiaryImgUrl()));

            // 作者是否点赞了这条日记
            List<DiaryDianzan> diaryDianzans = diaryDianzanMapper.selectList(new LambdaQueryWrapper<DiaryDianzan>()
                    .eq(DiaryDianzan::getDiaryAid, diary.getAid())
                    .eq(DiaryDianzan::getUserAid, JwtUtil.getTokenId(request.getHeader("token")))
            );
            resultItem.setIsLike(diaryDianzans.size() > 0);

            // 这条日记的获赞数
            resultItem.setLikesNumber(diary.getLikesNumber());
            // 这条日记的作者是否是当前登陆的用户
            resultItem.setIsCurrentUser((JwtUtil.getTokenId(request.getHeader("token")).equals(diary.getUserAid())));

            result.add(resultItem);
        }
        return ResponseWrapper.markSuccess(result);
    }

    /**
     * 获取当前登陆这个用户关注的所有用户的所有日记列表
     *
     * @param request 参数
     * @return ResponseWrapper
     */
    @Override
    public ResponseWrapper getCurrentUserConcernUserDiaryList(HttpServletRequest request) {

        // 获取当前登录用户的aid
        Integer currentUserAid = JwtUtil.getTokenId(request.getHeader("token"));

        // 先把当前登陆用户关注的用户查出来
        List<UserGuanzhu> guanzhus = userGuanzhuMapper.selectList(new LambdaQueryWrapper<UserGuanzhu>()
                .eq(UserGuanzhu::getGuanzhuUserAid, currentUserAid)
        );

        ArrayList<Diary> diaries = new ArrayList<>();
        for (UserGuanzhu userGuanzhu : guanzhus) {
            diaries.addAll(diaryMapper.selectList(new LambdaQueryWrapper<Diary>()
                    .eq(Diary::getUserAid,userGuanzhu.getBeiguanzhuUserAid())
            ));
        }
        // 将diaries按照日期降序
        diaries.sort((t1, t2) -> t2.getCreateTime().compareTo(t1.getCreateTime()));

        // 返回结果
        ArrayList<GetRecommendDiaryList> result = new ArrayList<>();

        for (Diary diary : diaries) {
            GetRecommendDiaryList resultItem = new GetRecommendDiaryList();
            resultItem.setDiaryAid(diary.getAid());
            User user = userMapper.selectById(diary.getUserAid());
            resultItem.setUserAid(user.getAid());
            resultItem.setUserName(user.getUserName());
            resultItem.setUserNickName(user.getNickName() == null ? user.getUserName() : user.getNickName());
            resultItem.setAvatar(user.getAvatar() == null ? "https://img1.baidu.com/it/u=3866475743,1893693583&fm=253&fmt=auto&app=138&f=JPEG?w=400&h=400" : user.getAvatar());

            // 日记的发布时间(2022年12月1日 14:52)
            LocalDateTime time = diary.getCreateTime();
            // LocalDateTime转yyyy-MM-dd
            String time1 = ToolForDateTime.LocalDateTimeToStringToDate(time);
            // yyyy-MM-dd转x年x月x日
            String timer2 = ToolForDateTime.StringDateToYearMonthDay(time1);
            // 时间部分
            String time3 = time.toString().substring(11, 16);
            // 最后返回的发布时间
            resultItem.setTime(timer2 + " " + time3);

            // 这条日记的内容
            resultItem.setContent(diary.getContent() == null ? "" : diary.getContent());

            // 这条日记的图片list
            // 日记包含的图片地址（有可能没有，有可能只有一张，有可能有一张以上：地址之间以;隔开）
            resultItem.setImageList(Medthods.imageStrSplitToArrayList(diary.getDiaryImgUrl()));

            // 作者是否点赞了这条日记
            List<DiaryDianzan> diaryDianzans = diaryDianzanMapper.selectList(new LambdaQueryWrapper<DiaryDianzan>()
                    .eq(DiaryDianzan::getDiaryAid, diary.getAid())
                    .eq(DiaryDianzan::getUserAid, JwtUtil.getTokenId(request.getHeader("token")))
            );
            resultItem.setIsLike(diaryDianzans.size() > 0);

            // 这条日记的获赞数
            resultItem.setLikesNumber(diary.getLikesNumber());

            result.add(resultItem);
        }
        return ResponseWrapper.markSuccess(result);
    }

    /**
     * 添加日记
     *
     * @param request 参数
     * @param param 参数
     * @return ResponseWrapper
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseWrapper addOrEditDiary(HttpServletRequest request, AddDiaryParam param) {

        if (param.getDiariAid().equals(-1)){
            int insert = diaryMapper.insert(new Diary() {{
                setUserAid(JwtUtil.getTokenId(request.getHeader("token")));
                setDiaryImgUrl(param.getImageUrls());
                setContent(param.getContent());
                setLikesNumber(0);
                setPublished(param.getPublished());
                setTag(param.getTag());
            }});
            if (insert > 0){
                if (param.getPublished().equals(0)){
                    return ResponseWrapper.markCustomSuccess("日记已保存为草稿！");
                }else {
                    return ResponseWrapper.markCustomSuccess("日记发表成功！");
                }
            }else {
                return ResponseWrapper.markCustomError("添加失败！");
            }
        }else {
            Diary diary = diaryMapper.selectById(param.getDiariAid());
            diary.setContent(param.getContent());
            diary.setTag(param.getTag());
            diary.setDiaryImgUrl(param.getImageUrls());
            diary.setPublished(param.getPublished());
            int updateById = diaryMapper.updateById(diary);
            if (updateById > 0){
                return ResponseWrapper.markCustomSuccess("日记更新成功！");
            }else {
                return ResponseWrapper.markCustomError("更新失败！");
            }
        }



    }

    /**
     * 获取日记评论的树
     * @return ResponseWrapper
     */
    public List<DiaryComment> getDiaryCommentTree(Diary diary,Integer currentLoginUserAid){
        ArrayList<DiaryComment> diaryCommentList = new ArrayList<>();
        List<com.fzg.entity.DiaryComment> comments = diaryCommentMapper.selectList(new LambdaQueryWrapper<com.fzg.entity.DiaryComment>()
                .eq(com.fzg.entity.DiaryComment::getDiaryAid, diary.getAid())
                .orderByDesc(com.fzg.entity.DiaryComment::getCreateTime)
        );
        for (com.fzg.entity.DiaryComment comment : comments) {
            DiaryComment diaryComment = new DiaryComment();
            diaryComment.setCommentAid(comment.getAid());
            diaryComment.setCommentUserAid(comment.getUserAid());
            User user1 = userMapper.selectById(comment.getUserAid());
            if (user1.getAvatar() != null){
                diaryComment.setCommentUserAvatar(user1.getAvatar());
            }else {
                diaryComment.setCommentUserAvatar("https://img2.baidu.com/it/u=180286244,263138673&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
            }
            // 评论这条日记人的昵称
            if (user1.getNickName() == null){
                diaryComment.setNickName(user1.getUserName());
            }else {
                diaryComment.setNickName(user1.getNickName());
            }
            diaryComment.setCommentLikesCount(comment.getLikesCount());
            // 评论时间（格式：2022年12月1日 14:52）
            diaryComment.setCommentTime(
                    ToolForDateTime.StringDateToYearMonthDay(
                            ToolForDateTime.LocalDateTimeToStringToDate(comment.getCreateTime())
                    ) + " " + comment.getCreateTime().toString().substring(11, 16)
            );
            // 当前登陆用户是否给这条评论点赞了
            List<DiaryCommentDianzan> diaryCommentDianzans = diaryCommentDianzanMapper.selectList(new LambdaQueryWrapper<DiaryCommentDianzan>()
                    .eq(DiaryCommentDianzan::getUserAid, currentLoginUserAid)
            );
            boolean isUserDianZanDiaryComment = false;
            for (DiaryCommentDianzan diaryCommentDianzan : diaryCommentDianzans) {
                if (diaryCommentDianzan.getDiaryCommentAid().equals(comment.getAid())){
                    isUserDianZanDiaryComment = true;
                    break;
                }
            }
            diaryComment.setIsCommentDianzan(isUserDianZanDiaryComment);

            // 这条评论是否是作者自己评论的
            diaryComment.setIsAuthorSelf(comment.getUserAid().equals(diary.getUserAid()));

            // 评论的内容
            diaryComment.setCommentContent(comment.getComment());

            // 评论的parentAid
            diaryComment.setParentAid(comment.getParentAid());

            // 父评论用户的名字和aid
            if (comment.getParentAid() != null){
                User user = userMapper.selectById(diaryCommentMapper.selectById(comment.getParentAid()).getUserAid());
                diaryComment.setParentCommentUserAid(user.getAid());
                if (user.getNickName() != null){
                    diaryComment.setParentCommentUserName(user.getNickName());
                }else {
                    diaryComment.setParentCommentUserName(user.getUserName());
                }
            }else {
                diaryComment.setParentCommentUserAid(null);
                diaryComment.setParentCommentUserName(null);
            }

            diaryCommentList.add(diaryComment);
        }

        //最高级别评论集合
        List<DiaryComment> roots = new ArrayList<>();
        List<DiaryComment> res = new ArrayList<>();

        for (DiaryComment comment : diaryCommentList) {
            //null表示最高级别的用户
            if(comment.getParentAid() == null)
            {
                roots.add(comment);
            }
        }
        //从最高级别用户开始遍历，递归找到该评论的下级评论，将带有下级的最高级评论放入返回结果中
        for(DiaryComment comment :roots)
        {
            comment=buildCommentTree(diaryCommentList,comment);
            res.add(comment);
        }
        return res;
    }


    /*
    功能：根据传入的评论，获取该评论的下属集合，将集合赋值给该评论的children属性，返回该评论
     */
    public DiaryComment buildCommentTree(List<DiaryComment> diaryCommentList,DiaryComment comment){
        List<DiaryComment> children = new ArrayList<>();
        //遍历查到的所有评论
        for(DiaryComment comment1:diaryCommentList)
        {
            //-1代表根节点，无需重复比较
            if(comment1.getParentAid() == null)
                continue;
            //当前评论的上级编号和传入的评论编号相等，表示该评论是传入评论的下级评论
            if(comment1.getParentAid().equals(comment.getCommentAid()))
            {
                //递归调用，再去寻找该评论的下级评论
                comment1=buildCommentTree(diaryCommentList,comment1);
                //当前评论是该评论的一个下级评论，放入children集合内
                children.add(comment1);
            }
        }
        //给该评论的children属性赋值，并返回该评论
        comment.setChildCommentList(children);
        return comment;
    }


}
